package com.ruoyi.web.controller.vantController.paymentAlipayController;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.baomidou.mybatisplus.extension.api.R;
import com.ruoyi.common.config.AliDevPayConfig;
import com.ruoyi.system.domain.zhuangxiumain.orderformMain.OrderMaster;
import com.ruoyi.system.domain.zhuangxiumain.vo.AfterSaleVo;
import com.ruoyi.system.service.vantservice.orderservice.ApiOrderMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/api/index")
@CrossOrigin
public class AlipayController {

    @Autowired
    private ApiOrderMasterService apiOrderMasterService;
    /**
     * 充值
     *
     * @return R
     */
    @PostMapping(value = "/charge")
    public R charge(@RequestBody OrderMaster orderMaster){
        //payDto就是你前端传过来的值,比如充值金额是多少 用户id是多少..
        //这个实体类根据你们自己的业务需求去写就好
        String orderString = apiOrderMasterService.order(orderMaster);
        return R.ok(orderString);
    }


    @ResponseBody
    @RequestMapping("/pay")
    public String pay(String out_trade_no,String subject,String total_amount,
                      String body,String timeout_expressm,String product_ode) throws AlipayApiException {

        AliDevPayConfig aliDevPayConfig = new AliDevPayConfig();

        AlipayClient alipayClient = new DefaultAlipayClient(
                aliDevPayConfig.URL,aliDevPayConfig.APPID,aliDevPayConfig.RSA_PRIVATE_KEY,
                aliDevPayConfig.FORMAT,aliDevPayConfig.CHARSET,
                aliDevPayConfig.ALIPAY_PUBLIC_KEY,aliDevPayConfig.SIGNTYPE
        );
        // 创建  Request请求
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();

        //封装传入参数
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(out_trade_no);
        model.setSubject(subject);
        model.setTotalAmount(total_amount);
        model.setBody(body);
        model.setTimeExpire(timeout_expressm);
        model.setProductCode(product_ode);

        //设置参数
        request.setBizModel(model);
        //设置异步回调地址
        request.setNotifyUrl(aliDevPayConfig.notify_url);
        //设置同步回调地址
        request.setReturnUrl(aliDevPayConfig.return_url);
        //生成表单
        String form = alipayClient.pageExecute(request).getBody();
        System.err.println(form);
        return form;
    }


}
